<?php

namespace IDock\Insurance\MACN\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\MACN\MACNEnums;
use IDock\Printer\VehicleModelPrinter;
use IDock\Printer\VehiclePrinter;

class VehicleModelResponse extends AllMACNResponse
{
    public function resolve(): Response
    {
        $body = $this->getBody();
        $body = iconv('UTF-8','GBK',$body);
        $this->printer = new VehicleModelPrinter();
        try {
            if ($body) {
                libxml_disable_entity_loader(false);
                $body = simplexml_load_string($body);
                $body = $this->objectToArray($body);
                $errorCode = $body['head']['statusCode'] ?? '';
                if ($errorCode !== 'success' && $errorCode !== '000000') {
                    $this->setError($body['head']['description'] ?? '未知错误或无错误提示');
                    return  $this;
                }
                $this->setBody($body['body'] ?? '');
            }

            return $this;
        } catch (\Throwable $exception) {
            var_dump($this->getBody());
        }
        $this->setError('亚太格式解析失败');
        return $this;
    }

    public function printerInit(): Response
    {
        $vehicleModel= $this->getBody();

        $vehicleModelPrinter = $this->printer;
        $modelList = $vehicleModel['vehiclePriceList']['vehiclePriceInfo'] ?? '';
        if(!is_array($modelList[0] ?? '')){
            $tmp = $modelList;
            unset($modelList);
            $modelList[0] = $tmp;
        }

        foreach ($modelList as $datum) {
            $vehiclePrinter = new VehiclePrinter();
            $vehiclePrinter->item['carKind'] = $datum['carKind'] ?? '';
            $vehiclePrinter->vehicleModelCode = $datum['vehicleCode'] ?? '';
            $vehiclePrinter->brandName = $datum['carBrandName'] ?? '';
            $vehiclePrinter->vehicleExhaust = $datum['exhaustCapacity'] ?? '';

            $vehiclePrinter->isNewEnergy = ($datum['fuelType'] ?? '') == '0' ? '0' : '1';
            $vehiclePrinter->vehicleName = $datum['vehicleName'] ?? '';
            $vehiclePrinter->vehiclePrice = $datum['vehiclePrice'] ?? '';
            $vehiclePrinter->vehiclePriceWithOutTax = $datum['vehiclePrice'] ?? '';
            $vehiclePrinter->vehicleSeat = $datum['limitLoadPerson'] ?? '';
            $vehiclePrinter->remark = '';

            $vehiclePrinter->familyName = $datum['vehicleFamily'] ?? '';
            $vehiclePrinter->power = $datum['power'] ?? '';
            $vehiclePrinter->vehicleTonnage = $datum['vehicleTonnage'] ?? '';
            $vehiclePrinter->wholeWeight = $datum['completeKerbMass'] ?? '';
            $vehiclePrinter->energyTypeCode = MACNEnums::$energyType[$datum['fuelType'] ?? ''] ?? '';
            $vehiclePrinter->energyTypeName = IDockEnums::$energyType[$vehiclePrinter->energyTypeCode] ?? '';
            $vehiclePrinter->item['vehicleId'] = $datum['plat_vehicleStyleUniqueId'] ?? '';

            //$vehiclePrinter->item = $datum;

            $vehicleModelPrinter->setModelList($vehiclePrinter);
        }

        $this->printer = $vehicleModelPrinter;

        return $this;
    }
}
